package com.youyiwen.Controller;

import com.youyiwen.Bean.User;
import com.youyiwen.Mapper.UserMapper;
import com.youyiwen.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.thymeleaf.util.StringUtils;

/**
 * @Author: zhaoyang
 * @Date: 2021/03/20
 */
@Controller
public class RegistController {

    @Autowired
    UserMapper userMapper;

    @GetMapping("/register")
    public String registerPage() {
        return "registration";
    }

    @PostMapping("/register")
    public String register(User user, Model model,
                           @RequestParam("confirm") String confirm,
                           @RequestParam("role") String role) {

        if (!StringUtils.isEmpty(user.getUserName()) && !StringUtils.isEmpty(user.getPassword())) {
            if (userMapper.selectUserByName(user.getUserName()) == null) {
                user.setRole(role);
                userMapper.insertUser(user);
                return "login";
            }
        }
        //model设置回显
        model.addAttribute("userName",user.getUserName());
        model.addAttribute("password",user.getPassword());
        model.addAttribute("telephone",user.getTelephone());
        model.addAttribute("email",user.getEmail());
        model.addAttribute("city",user.getCity());
        model.addAttribute("fullName",user.getFullName());
        model.addAttribute("confirm",confirm);
        model.addAttribute("msg", "用户名已存在，请重新输入");
        return "registration";
    }

}
